测试编解码器

编码器和解码器完成,但仍有一些缺失:测试。

没有测试你只看到如果编解码器工作对一些真正的服务器运行时,这并不是你应该是依靠什么。第十章所示,为一个自定义编写测试 ChannelHandler通常是通过 EmbeddedChannel。

所以这正是现在做测试我们定制的编解码器,其中包括一个编码器和解码器。让重新开始编码器。后面的清单显示了简单的编写单元测试。

Listing 14.5 MemcachedRequestEncoderTest class

public class MemcachedRequestEncoderTest {

  1. @Test
  2. public void testMemcachedRequestEncoder() {
  3. MemcachedRequest request = new MemcachedRequest(Opcode.SET, "key1", "value1"); //1
  4. EmbeddedChannel channel = new EmbeddedChannel(new MemcachedRequestEncoder()); //2
  5. channel.writeOutbound(request); //3
  6. ByteBuf encoded = (ByteBuf) channel.readOutbound();
  7. Assert.assertNotNull(encoded); //4
  8. Assert.assertEquals(request.magic(), encoded.readUnsignedByte()); //5
  9. Assert.assertEquals(request.opCode(), encoded.readByte()); //6
  10. Assert.assertEquals(4, encoded.readShort());//7
  11. Assert.assertEquals((byte) 0x08, encoded.readByte()); //8
  12. Assert.assertEquals((byte) 0, encoded.readByte());//9
  13. Assert.assertEquals(0, encoded.readShort());//10
  14. Assert.assertEquals(4 + 6 + 8, encoded.readInt());//11
  15. Assert.assertEquals(request.id(), encoded.readInt());//12
  16. Assert.assertEquals(request.cas(), encoded.readLong());//13
  17. Assert.assertEquals(request.flags(), encoded.readInt()); //14
  18. Assert.assertEquals(request.expires(), encoded.readInt()); //15
  19. byte[] data = new byte[encoded.readableBytes()]; //16
  20. encoded.readBytes(data);
  21. Assert.assertArrayEquals((request.key() + request.body()).getBytes(CharsetUtil.UTF_8), data);
  22. Assert.assertFalse(encoded.isReadable()); //17
  23. Assert.assertFalse(channel.finish());
  24. Assert.assertNull(channel.readInbound());
  25. }

}

  1. 新建 MemcachedRequest 用于编码为 ByteBuf
  2. 新建 EmbeddedChannel 用于保持 MemcachedRequestEncoder 到测试
  3. 写请求到 channel 并且判断是否产生了编码的消息
  4. 检查 ByteBuf 是否 null
  5. 判断 magic 是否正确写入 ByteBuf
  6. 判断 opCode (SET) 是否写入正确
  7. 检查 key 是否写入长度正确
  8. 检查写入的请求是否额外包含
  9. 检查数据类型是否写
  10. 检查是否保留数据插入
  11. 检查 body 的整体大小 计算方式是 key.length + body.length + extras
  12. 检查是否正确写入 id
  13. 检查是否正确写入 Compare and Swap (CAS)
  14. 检查是否正确的 flag
  15. 检查是否正确设置到期时间的
  16. 检查 key 和 body 是否正确
  17. 检查是否可读

Listing 14.6 MemcachedResponseDecoderTest class

  1. public class MemcachedResponseDecoderTest {
  2. @Test
  3. public void testMemcachedResponseDecoder() {
  4. EmbeddedChannel channel = new EmbeddedChannel(new MemcachedResponseDecoder()); //1
  5. byte magic = 1;
  6. byte opCode = Opcode.SET;
  7. byte[] key = "Key1".getBytes(CharsetUtil.US_ASCII);
  8. byte[] body = "Value".getBytes(CharsetUtil.US_ASCII);
  9. int id = (int) System.currentTimeMillis();
  10. long cas = System.currentTimeMillis();
  11. ByteBuf buffer = Unpooled.buffer(); //2
  12. buffer.writeByte(magic);
  13. buffer.writeByte(opCode);
  14. buffer.writeShort(key.length);
  15. buffer.writeByte(0);
  16. buffer.writeByte(0);
  17. buffer.writeShort(Status.KEY_EXISTS);
  18. buffer.writeInt(body.length + key.length);
  19. buffer.writeInt(id);
  20. buffer.writeLong(cas);
  21. buffer.writeBytes(key);
  22. buffer.writeBytes(body);
  23. Assert.assertTrue(channel.writeInbound(buffer)); //3
  24. MemcachedResponse response = (MemcachedResponse) channel.readInbound();
  25. assertResponse(response, magic, opCode, Status.KEY_EXISTS, 0, 0, id, cas, key, body);//4
  26. }
  27. @Test
  28. public void testMemcachedResponseDecoderFragments() {
  29. EmbeddedChannel channel = new EmbeddedChannel(new MemcachedResponseDecoder()); //5
  30. byte magic = 1;
  31. byte opCode = Opcode.SET;
  32. byte[] key = "Key1".getBytes(CharsetUtil.US_ASCII);
  33. byte[] body = "Value".getBytes(CharsetUtil.US_ASCII);
  34. int id = (int) System.currentTimeMillis();
  35. long cas = System.currentTimeMillis();
  36. ByteBuf buffer = Unpooled.buffer(); //6
  37. buffer.writeByte(magic);
  38. buffer.writeByte(opCode);
  39. buffer.writeShort(key.length);
  40. buffer.writeByte(0);
  41. buffer.writeByte(0);
  42. buffer.writeShort(Status.KEY_EXISTS);
  43. buffer.writeInt(body.length + key.length);
  44. buffer.writeInt(id);
  45. buffer.writeLong(cas);
  46. buffer.writeBytes(key);
  47. buffer.writeBytes(body);
  48. ByteBuf fragment1 = buffer.readBytes(8); //7
  49. ByteBuf fragment2 = buffer.readBytes(24);
  50. ByteBuf fragment3 = buffer;
  51. Assert.assertFalse(channel.writeInbound(fragment1)); //8
  52. Assert.assertFalse(channel.writeInbound(fragment2)); //9
  53. Assert.assertTrue(channel.writeInbound(fragment3)); //10
  54. MemcachedResponse response = (MemcachedResponse) channel.readInbound();
  55. assertResponse(response, magic, opCode, Status.KEY_EXISTS, 0, 0, id, cas, key, body);//11
  56. }
  57. private static void assertResponse(MemcachedResponse response, byte magic, byte opCode, short status, int expires, int flags, int id, long cas, byte[] key, byte[] body) {
  58. Assert.assertEquals(magic, response.magic());
  59. Assert.assertArrayEquals(key, response.key().getBytes(CharsetUtil.US_ASCII));
  60. Assert.assertEquals(opCode, response.opCode());
  61. Assert.assertEquals(status, response.status());
  62. Assert.assertEquals(cas, response.cas());
  63. Assert.assertEquals(expires, response.expires());
  64. Assert.assertEquals(flags, response.flags());
  65. Assert.assertArrayEquals(body, response.data().getBytes(CharsetUtil.US_ASCII));
  66. Assert.assertEquals(id, response.id());
  67. }
  68. }
  1. 新建 EmbeddedChannel ,持有 MemcachedResponseDecoder 到测试
  2. 创建一个新的 Buffer 并写入数据,与二进制协议的结构相匹配
  3. 写缓冲区到 EmbeddedChannel 和检查是否一个新的MemcachedResponse 创建由声明返回值
  4. 判断 MemcachedResponse 和预期的值
  5. 创建一个新的 EmbeddedChannel 持有 MemcachedResponseDecoder 到测试
  6. 创建一个新的 Buffer 和写入数据的二进制协议的结构相匹配
  7. 缓冲分割成三个片段
  8. 写的第一个片段 EmbeddedChannel 并检查,没有新的MemcachedResponse 创建,因为并不是所有的数据都是准备好了
  9. 写第二个片段 EmbeddedChannel 和检查,没有新的MemcachedResponse 创建,因为并不是所有的数据都是准备好了
  10. 写最后一段到 EmbeddedChannel 和检查新的 MemcachedResponse
    是否创建,因为我们终于收到所有数据
  11. 判断 MemcachedResponse 与预期的值